import storage from 'store';
import {
 postLogin, getInfo, getUserInfo, logout, login
} from '@/api/flow/auth';
import { register } from '@/api/flow/user';
import { ACCESS_TOKEN, ACCESS_RISK_TOKEN } from '@/store/mutation-types';
import roles from '@/utils/roles';

const user = {
  state: {
    token: '',
    r_token: '',
    name: '',
    welcome: '',
    avatar: '',
    roles: [],
    info: {},
    authentication: false
  },

  mutations: {
    SET_AUTHENTICATION(state, auth) {
      state.authentication = auth;
    },
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_RISK_TOKEN: (state, token) => {
      state.r_token = token;
    },
    SET_NAME: (state, { name, welcome }) => {
      state.name = name;
      state.welcome = welcome;
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar;
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles;
    },
    SET_INFO: (state, info) => {
      state.info = info;
    }
  },

  actions: {
    Register({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        register(userInfo).then((response) => {
          console.log('res:', response);
          const { token, m_token } = response;

          // flowengine token
          storage.set(ACCESS_TOKEN, token);
          commit('SET_TOKEN', token);

          // risk token
          // commit('SET_RISK_TOKEN', m_token);
          // storage.set(ACCESS_RISK_TOKEN, m_token);
          resolve();
        }).catch((error) => {
          reject(error);
        });
      });
    },

    // 登录
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        console.log(userInfo, ' dadadada');
        login(userInfo).then((response) => {
          console.log('res:', response);
          const { token, m_token } = response;

          // flowengine token
          storage.set(ACCESS_TOKEN, token);
          commit('SET_TOKEN', token);

          // risk token
          // commit('SET_RISK_TOKEN', m_token);
          // storage.set(ACCESS_RISK_TOKEN, m_token);
          resolve();
        }).catch((error) => {
          reject(error);
        });
      });
    },

    // 获取用户信息
    GetInfo({ commit }) {
      return new Promise((resolve, reject) => {
        getUserInfo().then((response) => {
          // if (response.role && response.role.permissions.length > 0) {
          //   // const role = response.role
          //   // role.permissions = response.role.permissions
          //   // role.permissions.map(per => {
          //   //   if (per.actionEntitySet != null && per.actionEntitySet.length > 0) {
          //   //     const action = per.actionEntitySet.map(action => { return action.action })
          //   //     per.actionList = action
          //   //   }
          //   // })
          //   // role.permissionList = role.permissions.map(permission => { return permission.permissionId })
          //   // commit('SET_ROLES', 'admin')
          //   commit('SET_INFO', response)
          // } else {
          //   // reject(new Error('getInfo: roles must be a non-null array !'))
          // }

          const role = roles;
          role.permissions = roles.permissions;
          role.permissions.map((per) => {
            if (per.actionEntitySet != null && per.actionEntitySet.length > 0) {
              const action = per.actionEntitySet.map((action) => action.action);
              per.actionList = action;
            }
          });

          role.permissionList = role.permissions.map((permission) => permission.permissionId);

          commit('SET_INFO', response);

          commit('SET_NAME', { name: response.last_name, welcome: '' });

          // AUTHENTICATION
          const AUTHENTICATION = (!!response.mobile && response.personal_info_status && response.user_bank_card_status);
          commit('SET_AUTHENTICATION', AUTHENTICATION);

          // commit('SET_AVATAR', response.avatar);

          resolve({ ...response, role });
        }).catch((error) => {
          reject(error);
        });
      });
    },

    // 登出
    Logout({ commit, state }) {
      return new Promise((resolve) => {
        logout(state.token).then(() => {
          resolve();
        }).catch(() => {
          resolve();
        }).finally(() => {
          commit('SET_TOKEN', '');
          // commit('SET_RISK_TOKEN', '');
          commit('SET_ROLES', []);
          storage.remove(ACCESS_TOKEN);
          // storage.remove(ACCESS_RISK_TOKEN);
        });
      });
    }

  }
};

export default user;
